package com.sz.demo.model;

import lombok.Data;

import java.math.BigDecimal;

/**
 * 商品抽象类
 */
@Data
public abstract class Product {
    protected String name;
    protected BigDecimal basePrice;
    protected String unit;
    /**
     * 折扣率，默认无折扣
     */
    protected BigDecimal discountRate = BigDecimal.ONE;

    public Product(String name, BigDecimal basePrice, String unit) {
        this.name = name;
        this.basePrice = basePrice;
        this.unit = unit;
    }

    public String getName() {
        return name;
    }

    public BigDecimal getBasePrice() {
        return basePrice;
    }

    public String getUnit() {
        return unit;
    }

    public BigDecimal getDiscountRate() {
        return discountRate;
    }

    public void setDiscountRate(BigDecimal discountRate) {
        if (discountRate.compareTo(BigDecimal.ZERO) >= 0 && discountRate.compareTo(BigDecimal.ONE) <= 0) {
            this.discountRate = discountRate;
        }else {
            throw new RuntimeException("折扣值有误");
        }
    }

    /**
     * 计算指定数量的商品价格（考虑折扣）
     */
    public BigDecimal calculatePrice(int quantity) {
        return basePrice.multiply(BigDecimal.valueOf(quantity)).multiply(discountRate);
    }

    /**
     * 获取折扣后的单价
     */
    public BigDecimal getDiscountedPrice() {
        return basePrice.multiply(discountRate);
    }
}